﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;
using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.admin
{
    public partial class adminRule : BaseCrm
    {
        readonly DALRule theRule = new DALRule();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly adminlogin theAdminLogin = new adminlogin();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(eRule.AdminManager);
                this.BuildRuleList();
            }
        }

        private void BuildRuleList()
        {
            StringBuilder strHead = new StringBuilder();
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");

            DataSet dsSub = new DataSet();
            string sAdminID = Request.QueryString["uid"];
            dsSub = theRule.GetListByErpLevel(0);
            strTable.Append("<div class='div_admin_list'>");
            int iColumn = 0;

            foreach (DataRow dr in dsSub.Tables[0].Rows)
            {
                if (iColumn < dsSub.Tables[0].Rows.Count)
                {
                    if (iColumn % 5 == 0)
                    {
                        strBody.Append("<div class='list'>");
                    }
                    string sChecked = "";
                    if (theRule.GetRule(int.Parse(dr["ruleID"].ToString()), int.Parse(sAdminID)))
                    {
                        sChecked = "checked='checked'";
                        strCheckedAdmin.Append(dr["ruleID"] + ",");
                    }
                    strBody.Append("<div class='content'><input type='checkbox' onclick='setAdminId(this.id)' " + sChecked + " id='" + dr["ruleID"].ToString() + "'/>" + dr["Description"].ToString() +"</div>");

                    iColumn++;
                    if (iColumn % 5 == 0)
                    {
                        strBody.Append("</div>");
                    }
                 }
            }

            strTable.Append(strHead.ToString());
            strTable.Append(strBody.ToString());
            strTable.Append("</div>");

            this.ltRuleList.Text = strTable.ToString();
            this.hdList.Value = strCheckedAdmin.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sRuleIds = string.Empty;
            if (this.hdList.Value.Length > 1)
            {
                sRuleIds = this.hdList.Value.Substring(1, this.hdList.Value.Length - 2);
            }
            else
            {
                sRuleIds = "0,0";
            }

            theUserRuleRef.Save(int.Parse(Request.QueryString["uid"]), sRuleIds);
            ExtAspNet.Alert.Show("保存成功！", "", "openNewTab('AdminManager','/crm/admin/adminManager.aspx','管理员管理','AdminPermission')");
        }
    }
}